package Pages.Course;

import Data.ClassJTable;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ResourceBundle;

import static Data.ClassJTable.fillTable;

/**
 * description:
 * author: 柳胜酉
 * 2022/7/8
 */
public class CourseSelectPageT extends JFrame {
    private JPanel dialogPane;
    private JPanel contentPanel;
    private JPanel panel1;
    private JPanel panel2;
    private JLabel label1;
    private JPanel panel3;
    private JLabel label2;
    private JTextField textField1;
    private JPanel panel4;
    private JLabel label3;
    private JScrollPane scrollPane1;
    private JTable table1;
    private JPanel buttonBar;
    private JButton okButton;
    private JButton cancelButton;
    private DefaultTableModel dtm = null;
    String table = "user_course";
    public CourseSelectPageT() {
        initComponents();
    }

    private void initComponents() {
        ResourceBundle bundle = ResourceBundle.getBundle("P0704");
        dialogPane = new JPanel();
        contentPanel = new JPanel();
        panel1 = new JPanel();
        panel2 = new JPanel();
        label1 = new JLabel();
        panel3 = new JPanel();
        label2 = new JLabel();
        textField1 = new JTextField();
        panel4 = new JPanel();
        label3 = new JLabel();
        scrollPane1 = new JScrollPane();
        table1 = new JTable();
        buttonBar = new JPanel();
        okButton = new JButton();
        cancelButton = new JButton();

        String[] head = {"班级", "年级", "课程", "教师"};
        dtm = new DefaultTableModel();
        //遍历数组，确定你有几列
        for (int i = 0; i < head.length; i++) {
            dtm.addColumn(head[i]);
        }
        fillTable(dtm,table);//表格内容的填充
        table1 = new JTable(dtm);
        scrollPane1 = new JScrollPane(table1);

        //======== this ========
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("\u67e5\u8be2\u8bfe\u7a0b\u4fe1\u606f");
        setResizable(false);
        var contentPane = getContentPane();
        contentPane.setLayout(new BorderLayout());

        //======== dialogPane ========
        {
            dialogPane.setBorder(new EmptyBorder(12, 12, 12, 12));
            dialogPane.setBackground(SystemColor.inactiveCaption);
            dialogPane.setLayout(new BorderLayout());

            //======== contentPanel ========
            {
                contentPanel.setLayout(new CardLayout());

                //======== panel1 ========
                {
                    panel1.setBackground(SystemColor.inactiveCaption);
                    panel1.setLayout(new BorderLayout());

                    //======== panel2 ========
                    {
                        panel2.setBackground(SystemColor.activeCaption);
                        panel2.setLayout(new GridLayout(2, 1));

                        //---- label1 ----
                        label1.setText(bundle.getString("CourseSelectPage.label1.text_2"));
                        label1.setHorizontalAlignment(SwingConstants.CENTER);
                        label1.setFont(label1.getFont().deriveFont(label1.getFont().getSize() + 20f));
                        label1.setBackground(SystemColor.inactiveCaption);
                        label1.setForeground(Color.white);
                        panel2.add(label1);

                        //======== panel3 ========
                        {
                            panel3.setBackground(SystemColor.inactiveCaption);
                            panel3.setLayout(new GridLayout(1, 2));

                            //---- label2 ----
                            label2.setText(bundle.getString("CourseSelectPage.label2.text_2"));
                            label2.setHorizontalAlignment(SwingConstants.CENTER);
                            label2.setFont(label2.getFont().deriveFont(label2.getFont().getSize() + 15f));
                            label2.setForeground(Color.darkGray);
                            label2.setIcon(new ImageIcon(getClass().getResource("/image/xinrenkecheng.png")));
                            panel3.add(label2);

                            //---- textField1 ----
                            textField1.setFont(textField1.getFont().deriveFont(textField1.getFont().getSize() + 10f));
                            panel3.add(textField1);
                        }
                        panel2.add(panel3);
                    }
                    panel1.add(panel2, BorderLayout.NORTH);

                    //======== panel4 ========
                    {
                        panel4.setBackground(SystemColor.inactiveCaption);
                        panel4.setLayout(new BorderLayout());

                        //---- label3 ----
                        label3.setText(bundle.getString("CourseSelectPage.label3.text_2"));
                        label3.setFont(label3.getFont().deriveFont(label3.getFont().getSize() + 5f));
                        label3.setForeground(Color.darkGray);
                        panel4.add(label3, BorderLayout.NORTH);

                        //======== scrollPane1 ========
                        {
                            scrollPane1.setViewportView(table1);
                        }
                        panel4.add(scrollPane1, BorderLayout.CENTER);
                    }
                    panel1.add(panel4, BorderLayout.CENTER);
                }
                contentPanel.add(panel1, "card1");
            }
            dialogPane.add(contentPanel, BorderLayout.CENTER);

            //======== buttonBar ========
            {
                buttonBar.setBorder(new EmptyBorder(12, 0, 0, 0));
                buttonBar.setBackground(SystemColor.inactiveCaption);
                buttonBar.setLayout(new GridBagLayout());
                ((GridBagLayout)buttonBar.getLayout()).columnWidths = new int[] {0, 85, 80};
                ((GridBagLayout)buttonBar.getLayout()).columnWeights = new double[] {1.0, 0.0, 0.0};

                //---- okButton ----
                okButton.setText(bundle.getString("CourseSelectPage.okButton.text_2"));
                okButton.setFont(okButton.getFont().deriveFont(okButton.getFont().getSize() + 10f));
                buttonBar.add(okButton, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
                        GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                        new Insets(0, 0, 0, 5), 0, 0));

                //---- cancelButton ----
                cancelButton.setText(bundle.getString("CourseSelectPage.cancelButton.text_2"));
                cancelButton.setFont(cancelButton.getFont().deriveFont(cancelButton.getFont().getSize() + 10f));
                buttonBar.add(cancelButton, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0,
                        GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                        new Insets(0, 0, 0, 0), 0, 0));
            }
            dialogPane.add(buttonBar, BorderLayout.SOUTH);
        }
        contentPane.add(dialogPane, BorderLayout.CENTER);
        setSize(380, 360);
        setLocationRelativeTo(null);

        okButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                ClassJTable.getAllDate(table,textField1.getText());
                dtm.setRowCount(0);
                table1.updateUI();
                ClassJTable.fillTable(dtm,table,textField1.getText());
                textField1.setText("");
            }
        });

        cancelButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });
    }

    public static void main(String[] args) {
        new CourseSelectPageT().setVisible(true);
    }
}
